<template>
      <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="中间件" prop="netSysId"> 
            <el-select
                  v-model="queryParams.netSysId"
                  placeholder="中间件"
                  class="!w-240px" clearable
                >
                  <el-option
                    v-for="n in sysConfigList"
                    :key="n.id"
                    :label="n.community.name"
                    :value="n.id"
                  />
                </el-select>
          </el-form-item>
          <el-form-item label="" prop="keyWord">
            <el-input placeholder="请输入内容" v-model.trim="keyWord">
              <template #prepend>
                <el-select v-model="kType" placeholder="请选择" class="!w-120px">
                  <el-option label="宽带账号" value="netAccount" />
                  <el-option label="宽带用户" value="memberUserName" />
                </el-select>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery"
              ><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button
            >
            <el-button
              type="primary"
              plain
              @click="openForm('create')"
              v-hasPermi="['zwsj:net-account-config:create']"
            >
              <Icon icon="ep:plus" class="mr-5px" /> 新增
            </el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>

      <!-- 列表 -->
      <ContentWrap>
        <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
          <el-table-column label="编号" align="center" prop="id" width="60" />
          <el-table-column label="中间件" align="center" prop="netSysName" width="130" />
          <el-table-column label="账号信息" align="left" min-width="380">
            <template #default="scope">
              <div v-if="scope.row.lfAccountRespDTO">
                <div>地址：{{ scope.row.lfAccountRespDTO.addr }}</div>
                <div>姓名：{{ scope.row.lfAccountRespDTO.userName }}（绑定惠ID：{{ scope.row.memberUserName }}——{{ scope.row.memberUserId }}）</div>
                <div>账号：{{scope.row.lfAccountRespDTO.serverName}}（{{ scope.row.netAccount }}）</div>
                <div>手机：{{ scope.row.lfAccountRespDTO.phone }}</div>
              </div>
              <div v-else>
                <div>姓名：{{ scope.row.memberUserName }}</div>
                <div>账号：{{ scope.row.netAccount }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column  label="有效期" align="left" prop="oldNetExpiresTime"  width="260px">
            <template #default="scope">
              <div v-if="scope.row.lfAccountRespDTO">
                <div>旧有效期：{{ formatDate(scope.row.oldNetExpiresTime) }}</div>
                <div v-if="scope.row.lfAccountRespDTO">新有效期：{{ formatDate(scope.row.lfAccountRespDTO.expireTime) }}</div>
                <div>最近充值：{{ formatDate(scope.row.lastPayTime) }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="100">
            <template #default="scope">
              <el-button
                link
                type="danger"
                @click="handleDelete(scope.row.id)"
                v-hasPermi="['zwsj:net-account-config:delete']"
              >
                解绑
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <NetAccountConfigForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { formatDate } from '@/utils/formatTime'
import * as NetAccountConfigApi from '@/api/zwsj/netAccountConfig'
import { getNetSysConfigs } from '@/api/zwsj/netSysConfig'
import NetAccountConfigForm from './NetAccountConfigForm.vue'

defineOptions({ name: 'NetAccountConfig' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据

const keyWord = ref('')
const kType = ref('netAccount')
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  netSysId: null,
  memberUserId: null,
  netAccount: null,
  memberUserName: null
})
const queryFormRef = ref() // 搜索的表单
const sysConfigList = ref([])

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    queryParams.memberUserName = undefined
    queryParams.netAccount = undefined
    if ('memberUserName' == kType.value) {
      queryParams.memberUserName = keyWord.value
    } else {
      queryParams.netAccount = keyWord.value
    }
    const data = await NetAccountConfigApi.getNetAccountConfigPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
  sysConfigList.value = await getNetSysConfigs()
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number, commId?: number) => {
  formRef.value.open(type, id, commId)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await NetAccountConfigApi.deleteNetAccountConfig(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
